package com.copera.cn.kaka.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class AsyncImageLoader {
	private static String cachePath = "/mnt/sdcard/cardImages/";
	public Map<String,SoftReference<Bitmap>> imageCache = null;
	
	public AsyncImageLoader(){
		imageCache = new HashMap<String,SoftReference<Bitmap>>();
	}
	
	public Bitmap loadBitmap(final ImageView imageView, final String imageUrl, final HttpClient httpClient, final ImageCallBack imageCallBack){
		if(imageCache.containsKey(imageUrl)){
			Log.i("imageCache", imageUrl);
			SoftReference<Bitmap> reference = imageCache.get(imageUrl);
			Bitmap bitmap = reference.get();
			if(bitmap != null){
				return bitmap;
			}
		}else{
			String bitmapName = imageUrl.substring(imageUrl.lastIndexOf("/")+1);
			File cacheDir = new File(cachePath);
			Log.i("imageDir", cacheDir.isDirectory()+"");
			File[] cacheFiles = cacheDir.listFiles();
			boolean exist = false;
			if(cacheFiles != null){
				for(File cacheFile:cacheFiles){
					if(bitmapName.equals(cacheFile.getName())){
						Log.i("file", cacheFile.isFile()+"");
						exist = true;
						break;
					}
				}
				if(exist){
					Log.i("imageFile", cachePath+bitmapName);
					return BitmapFactory.decodeFile(cachePath+bitmapName);
				}
			}
			
		}
		
		final Handler handler = new Handler(){
			@Override
			public void handleMessage(Message msg) {
				if(msg != null && msg.obj!=null){
					imageCallBack.imageLoad(imageView, (Bitmap)msg.obj);
				}
			}
		};
		
		new Thread(){
			@Override
			public void run() {
				FileOutputStream fos = null;
				try {
					Log.i("imageHttp", imageUrl);
					InputStream bitmapIs = HttpUtils.getStreamFromUrl(imageUrl, httpClient);
					if(bitmapIs != null){
						Log.i("imageSize", bitmapIs.available()+"");
						Bitmap bitmap = BitmapFactory.decodeStream(bitmapIs);
						imageCache.put(imageUrl, new SoftReference<Bitmap>(bitmap));
						Message msg = handler.obtainMessage(0, bitmap);
						handler.sendMessage(msg);
						
						
						File dir = new File(cachePath);
						if(!dir.exists()){
							dir.mkdirs();
						}
						
						File bitmapFile = new File(cachePath+imageUrl.substring(imageUrl.lastIndexOf("/")+1));
						if(!bitmapFile.exists()){
							bitmapFile.createNewFile();
						}
						
						fos = new FileOutputStream(bitmapFile);
						bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
					}
					
				} catch (ClientProtocolException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}finally{
					try{
						if(fos != null){
							fos.close();
						}
					}catch (Exception e) {
					}
				}
			};
		}.start();
		
		return null;
	}
	
	
	public interface ImageCallBack{
		public void imageLoad(ImageView imageView, Bitmap bitmap);
		
	}
	
}
